package com.mantru.iotpot.setting;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.DateClass.Set_Seasoning_Calibration_Date;
import com.mantru.iotpot.R;
import com.mantru.iotpot.setting.Adapter.SetSeasoningCalibrationAdapter;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SetSeasoningCalibrationActivity extends AppCompatActivity {
    private ApplicationClass ac;
    private RecyclerView recyclerView;
    private List<Set_Seasoning_Calibration_Date> dates = new ArrayList<>();
    private SetSeasoningCalibrationAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_seasoning_calibration);
        ac = (ApplicationClass) getApplication();// 初始化 ApplicationClass
        findViewById(R.id.seasoning_calibration_set_back).setOnClickListener(v -> {
            finish();
        });
        recyclerView = findViewById(R.id.seasoning_calibration_set_recyclerview);
        getJson();//获取json数据
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SetSeasoningCalibrationAdapter(dates);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    // 获取Json数据并解析
    private void getJson() {
        String json_data = ac.GetSQLData_Nozzle_List();
        Log.d("SetSeasoningCalibrationActivity","json_data: "+ json_data);
        try {
            JSONObject jsonObject = new JSONObject(json_data);
            if (jsonObject.getBoolean("success")) {
                JSONArray itemData = jsonObject.getJSONArray("data");
                for (int i = 0; i < itemData.length(); i++) {
                    JSONObject item = itemData.getJSONObject(i);
                    String title1 = item.getString("title1");
                    String title2 = item.getString("title2");
                    dates.add(new Set_Seasoning_Calibration_Date(title1, title2));
                }
            }
        } catch (Exception e) {
            Log.e("SetSeasoningCalibrationActivity", "JSON parsing error", e);
        }
    }
}